package cn.gzy.controller;

import cn.gzy.dto.LoginUser;
import cn.gzy.service.BlogService;
import cn.gzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class PublicController {
    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;
    private final String pre = "forward:/statics/html";
    private final String suf = ".html";

    @PostMapping("/login")
    @ResponseBody
    public Map<String,String> login( @RequestBody LoginUser user){
        Map<String,String> token = new HashMap<String,String>();
        String t = Optional.ofNullable(userService.login(user)).orElse("");
        token.put("token",t);
        return token;
    }

//@GetMapping({"/blog/*","/index","/personInfo/*","/users/*","/register","/login"})
//public String common(HttpServletRequest req) {
//    String pre = "/html/"; // 确保这些变量被定义和初始化
//    String suf = ".html";
//
//    // 特殊处理/index，避免数组越界
//    if ("/index".equals(req.getRequestURI())) {
//        return pre + "index" + suf;
//    }
//
//    // 获取URI的第二部分，处理其他情况
//    String[] uriParts = req.getRequestURI().split("/");
//    if (uriParts.length > 2) {
//        String part = uriParts[1]; // 获取第二部分
//        return pre + part + suf;
//    } else {
//        // 返回一个默认或错误页面
//        return "error";
//    }
//}
@GetMapping({"/blog/*","/index","/personInfo/*","/users/*","/register","/login"})
public String common(HttpServletRequest req){
    System.out.println(pre + req.getRequestURI() + suf);
    // 去掉restful的参数，直接访问html文件
    return pre + "/"+req.getRequestURI().split("[/]")[1] + suf;
}

}
